import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By


# 爬取数据类
class AcquireProductData:
    driver = None

    # 类初始化函数
    def __init__(self, AccountPath="https://www.jd.com"):

        # 启动浏览器
        self.driver = webdriver.Chrome()
        self.driver.get(AccountPath)

    def searchProduct(self,startpage,endpage):
        for i in range(startpage,endpage+1):
            print("爬取 %d/%d\n", startpage, endpage)


    def getTotalpage(self):
        time.sleep(2)
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        totalpages = self.driver.find_element(By.XPATH,'//*[@id="J_bottomPage]/span[2]/em[1]/b').text()
        return totalpages

    def searchTag(self,tagName="华为手机"):
        self.driver.find_element(By.XPATH, '//*[@id="search"]/div[2]/div[2]/button').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH,'//*[@id="key"]').clear()
        self.driver.find_element(By.XPATH,'//*[@id="key"]').send_keys(tagName)
        time.sleep(2)
        self.driver.find_element(By.XPATH,'//*[@id="search"]/div[2]/div[2]/button').click()


